package repository

import (
	"gitee.com/ysl729895/go-mall/internal/model"
	"gorm.io/gorm"
)

// PermissionRepository 权限数据操作接口
//go:generate mockgen -source=permission_repository.go -destination=mock_permission_repository.go -package=repository

type PermissionRepository interface {
	Create(permission *model.Permission) error
	Update(permission *model.Permission) error
	Delete(id uint) error
	GetByID(id uint) (*model.Permission, error)
	GetAll() ([]model.Permission, error)
}

type permissionRepository struct {
	db *gorm.DB
}

func NewPermissionRepository(db *gorm.DB) PermissionRepository {
	return &permissionRepository{db: db}
}

func (r *permissionRepository) Create(permission *model.Permission) error {
	return r.db.Create(permission).Error
}

func (r *permissionRepository) Update(permission *model.Permission) error {
	return r.db.Save(permission).Error
}

func (r *permissionRepository) Delete(id uint) error {
	return r.db.Delete(&model.Permission{}, id).Error
}

func (r *permissionRepository) GetByID(id uint) (*model.Permission, error) {
	var permission model.Permission
	err := r.db.First(&permission, id).Error
	return &permission, err
}

func (r *permissionRepository) GetAll() ([]model.Permission, error) {
	var permissions []model.Permission
	err := r.db.Find(&permissions).Error
	return permissions, err
}
